package com.mlink.job.quartz.utils;

import com.base.commons.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 带数据类型查询参数工具类
 * 计划任务运行工厂类.
 * Created by Irany 2017/12/14 14:23
 */
public class SearchDataTypeUtils {
    /**
     * 获取参数数据类型.
     * @return 0:字符串；1:数值；2:日期
     */
    public static int getDataType(String typeCode) {
        if (StringUtils.isNotBlank(typeCode)) {
            if ("s".equalsIgnoreCase(typeCode)) {
                return 0;
            }
            if ("n".equalsIgnoreCase(typeCode)) {
                return 1;
            }
            if ("d".equalsIgnoreCase(typeCode)) {
                return 2;
            }
        }

        // 默认为字符串
        return 0;
    }

    public static Object converterData(int dataType, String[] values) {
        switch (dataType) {
            case 1:
                BigDecimal[] bigDecimals = new BigDecimal[values.length];

                for (int i=0; i < values.length; i++) {
                    bigDecimals[i] = (BigDecimal)converterData(dataType, values[i]);
                }

                return bigDecimals;
            case 2:
                Date[] dates = new Date[values.length];

                for (int i=0; i < values.length; i++) {
                    dates[i] = (Date)converterData(dataType, values[i]);
                }

                return dates;
            default:
                return values;
        }
    }

    public static Object converterData(int dataType, String value) {
        switch (dataType) {
            case 1:
                return new BigDecimal(value);
            case 2:
                try {
                    return DateUtils.toDate(value);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            default:
                return value;
        }
    }

    private SearchDataTypeUtils(){}
}
